package dgut.service;

import dgut.bean.Employee;

import dgut.bean.EmployeeExample;
import dgut.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpService {


    @Autowired //byType
    EmployeeMapper employeeMapper;


    public List<Employee> getAll() {

        EmployeeExample example = new EmployeeExample();
        example.setOrderByClause("emp_id ASC");
        return employeeMapper.selectByExampleWithDept(example);

    }

    public void saveEmps(Employee employee) {

        employeeMapper.insertSelective(employee);

    }

    public boolean checkUser(String empName) {

        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        return count == 0;


    }

    /*
    * 按照员工id查询员工信息
    * */
    public Employee getEmp(Integer id) {

        return employeeMapper.selectByPrimaryKey(id);

    }

    public int updateEmp(Employee employee) {

        //按照主键有选择的更新
        return employeeMapper.updateByPrimaryKeySelective(employee);

    }

    //单个删除
    public int deleteEmp(Integer id) {

        return employeeMapper.deleteByPrimaryKey(id);

    }

    //批量删除
    public int deleteEmp(List<Integer> idList) {

        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        //delete from xxx where emp_id in(1,2,3)
        criteria.andEmpIdIn(idList);
        return employeeMapper.deleteByExample(example);
    }
}
